package com.think.teaching.controller;

import com.think.common.controller.BaseController;
import com.think.common.model.Result;
import com.think.common.model.ResultCode;
import com.think.teaching.model.ExamPaperType;
import com.think.teaching.service.impl.ExamPaperTypeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/v1")
@Api(tags =  "试卷类型接口文档")
public class ExamPaperTypeController  extends BaseController{

	private final ExamPaperTypeServiceImpl examPaperTypeService;

	public ExamPaperTypeController(ExamPaperTypeServiceImpl examPaperTypeService) {
		this.examPaperTypeService = examPaperTypeService;
	}

	@ApiOperation("添加新的试卷类型")
	@PostMapping("admin/examPaperType")
	public Result AddExamPaperType(@RequestBody()ExamPaperType data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			
			ExamPaperType examPaperType = examPaperTypeService.GetExamPaperTypeByName(data.getTypeName());
			if (examPaperType != null) {
				result.setCode(ResultCode.EXAM_PAPER_TYPE_ALREADY_EXIST.code());
				result.setMsg(String.format("名为'%s'的类型已经存在", data.getTypeName()));//(ResultCode.COURSE_TYPE_ALREADY_EXIST);
				return result;
			}
			examPaperType = examPaperTypeService.AddExamPaperType(data);
			if( examPaperType!= null) {
				return Result.success(examPaperType);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("更新试卷类型")
	@RequestMapping(value = "admin/examPaperType",method = RequestMethod.PUT)
	public Result UpdateExamPaperType(@RequestBody()ExamPaperType data) {
		Result result = data.CheckObjectComplete();
		if(result.getCode() == ResultCode.SUCCESS.code()) {
			ExamPaperType examPaperType = examPaperTypeService.GetExamPaperTypeByName(data.getTypeName());
			if (examPaperType != null&&!data.getId().equals(examPaperType.getId())){
				result.setCode(ResultCode.EXAM_PAPER_TYPE_ALREADY_EXIST.code());
				result.setMsg(String.format("名为'%s'的类型已经存在", data.getTypeName()));//(ResultCode.COURSE_TYPE_ALREADY_EXIST);
				return result;
			}
			examPaperType = examPaperTypeService.UpdateExamPaperType(data);
			if( examPaperType!= null) {
				return Result.success(examPaperType);
			}
			else {
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
		}
		else
			return result;
	}
	
	@ApiOperation("删除试卷类型")
	@RequestMapping(value = "admin/examPaperType/{id}",method =
			RequestMethod.DELETE)
	public Result DeleteExamPaperType(@PathVariable("id")Integer id) {
		ExamPaperType examPaperType = examPaperTypeService.getById(id);
		if( examPaperType== null) {
			return Result.failure(ResultCode.EXAM_PAPER_TYPE_NOT_EXIST);
		}
		else {
			if (examPaperTypeService.DeleteObjectById(id)) {
				return Result.success();
			}
			return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
		}
	}
	
	@ApiOperation("获取试卷类型详情")
	@RequestMapping(value = "admin/examPaperType/{id}",method = RequestMethod.GET)
	public Result GetExamPaperType(@PathVariable("id")Integer id) {
		ExamPaperType examPaperType = examPaperTypeService.getById(id);
		if( examPaperType== null) {
			return Result.failure(ResultCode.EXAM_PAPER_TYPE_NOT_EXIST);
		}
		else {
			
			return Result.success(examPaperType);
		}
	}
	
	@ApiOperation("管理员修改试卷类型状态")
	@RequestMapping(value = "admin/examPaperType/{id}/{state}",method = RequestMethod.PUT)
	public Result UpdateExamPaperTypeState(@PathVariable("id")Integer id,@PathVariable("state")Integer state) {
		ExamPaperType examPaperType = examPaperTypeService.getById(id);
		if( examPaperType== null) {
			return Result.failure(ResultCode.EXAM_PAPER_TYPE_NOT_EXIST);
		}
		else {
			try {
				examPaperType = examPaperTypeService.SetExamPaperTypeState(id, state);
				return Result.success(examPaperType);
			} catch (Exception e) {
				return Result.failure(ResultCode.SYSTEM_DATABASE_ERROR);
			}
		}
	}
	
	@ApiOperation("用户搜索试卷类型")
	@RequestMapping(value = "user/examPaperType/search",method = RequestMethod.GET)
	public Result UserSearchExamPaperType(@RequestParam("key")String key) {
		return Result.success(examPaperTypeService.SearchExamPaperTypeWithKey(key, true));
	}
	
	@ApiOperation("管理员搜索试卷类型")
	@RequestMapping(value = "admin/examPaperType/search",method = RequestMethod.GET)
	public Result AdminSearchExamPaperType(@RequestParam("key")String key) {
		return Result.success(examPaperTypeService.SearchExamPaperTypeWithKey(key, false));
	}
	
	@ApiOperation("管理员获取试卷类型列表")
	@RequestMapping(value = "admin/examPaperTypes/{page}/{count}",method = RequestMethod.GET)
	public Result AdminListExamPaperType(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(examPaperTypeService.ListExamPaperTypeWithTagAndType(count, page, 0, 0, false));
	}
	
	@ApiOperation("用户获取试卷类型列表")
	@RequestMapping(value = "user/examPaperTypes/{page}/{count}",method = RequestMethod.GET)
	public Result UserListExamPaperType(@PathVariable("page")Integer page,@PathVariable("count")Integer count) {
		return Result.success(examPaperTypeService.ListExamPaperTypeWithTagAndType(count, page, 0, 0, true));
	}
}
